<template>
    <div class="product_list">
        <div class="header">
            <van-nav-bar title="供应商增类" left-text="" left-arrow @click-left="goBack" />
        </div>
        <div style="" class="product_list_serch-text">
            <van-search class="product_list_serch1" v-model="searchValue" shape="round" background="#F6FAFF" />
            <div class="product_list_serch2">
                <div style="">
                    <van-button class="product_list-btn" round @click="onSearch">搜索</van-button>
                </div>
            </div>
        </div>
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
            <van-list v-model:loading="loading" :finished="finished" @load="onLoad">
                <template v-slot:finished>
                    <div class="nothing_div" v-if="list.length == 0">
                        <van-image :src="nothingData"></van-image>
                        <div class="nothing_font">暂无数据</div>
                    </div>
                </template>
                <div class="product_list_van_list">
                    <div v-for="(item, index) in list" :key="index" class="product_list_van_list_div"
                        @click="handleGetDetails(item.id)">
                        <div class="div_right">
                            <div class="div_right_top">
                                <div class="style_name">
                                    {{ item.supplierName }}
                                </div>
                            </div>
                            <div class="div_right_center">
                                <div class="contract">
                                    <div :style="{
                                        color: item.sellStatus == '1' ? '#19D11B' : '#FC2540',
                                        backgroundColor: item.sellStatus == '1' ? '#E7FDEE' : '#FDE7E7',
                                    }">
                                        供销合同</div>
                                </div>
                                <div class="contract_break">
                                    <div :style="{
                                        color: item.packageStatus == '1' ? '#19D11B' : '#FC2540',
                                        backgroundColor: item.packageStatus == '1' ? '#E7FDEE' : '#FDE7E7',
                                    }">
                                        包销合同
                                    </div>
                                </div>
                                <div class="contract_break">
                                    <div :style="{
                                        color: item.depositStatus == '1' ? '#19D11B' : '#FC2540',
                                        backgroundColor: item.depositStatus == '1' ? '#E7FDEE' : '#FDE7E7',
                                    }">
                                        保证金协议
                                    </div>
                                </div>
                            </div>
                            <div class="div_right_bottom">
                                <div class="div_right_bottom_div">
                                    <div class="label">商品总数</div>
                                    <div class="value">{{ item.productsCount }}</div>
                                </div>
                                <div class="div_right_bottom_div">
                                    <div class="label">上架总数</div>
                                    <div class="value">{{ item.productsShopping }}</div>
                                </div>
                                <div class="div_right_bottom_div">
                                    <div class="label">选品数</div>
                                    <div class="value">{{ item.productsOpt }}</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </van-list>
        </van-pull-refresh>
    </div>
</template>
<script setup lang="ts">
import { computed, onMounted, ref, watch } from "vue";
import { hmSupplierList } from "@/api/supplierList/index";
import { useRoute, useRouter } from "vue-router";
const nothingData = new URL("@/assets/porduct/icon_zanwushuju.png", import.meta.url).href
const router = useRouter();
const route = useRoute()
const queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    supplierName: "",
});
const searchValue = ref("");

const refreshing = ref(false);
const loading = ref(false);
const finished = ref(false);
const list = ref<any[]>([]);

// 限制连续刷新数据重复问题
const refreFlag = ref(true)

/** 搜索 */
const onSearch = () => {
    queryParams.value.supplierName = searchValue.value;
    onRefresh();
};
/** 搜索供应商名称 */
const supplierName = computed(() => {
    return route.query.supplierName
})
watch(
    () => supplierName.value,
    (v: any) => {
        queryParams.value.supplierName = v;
        searchValue.value = v
    },
    { immediate: true }
);
/** 返回上一页 */
const goBack = () => {
    router.push('/researchAddSupplier')
}
/** 获取详情 */
const handleGetDetails = (id: string) => {
    router.push({
        path: `/researchAddSupplierClassAdd/${id}`,
        query: {
            supplierName: queryParams.value.supplierName
        }
    });
};
/** 查询商品列表 */
const getList = async () => {
    const res = await hmSupplierList(queryParams.value);
    refreFlag.value = true
    // 空数据 停止加载
    if (res.rows.length == 0) {
        list.value = [];
        finished.value = true;
        return;
    }
    // 有数据 赋值列表 加载状态结束
    res.rows.forEach((item) => {
        finished.value = false;
        list.value.push(item);
    });
    loading.value = false;
    refreshing.value = false;
    // 如果list长度大于等于总数据条数，数据全部加载完成
    if (list.value.length >= res.total) {
        finished.value = true; //结束加载
    }
};
/** 加载更多 */
const onLoad = async () => {
    getList();
    queryParams.value.pageNum++;
};
/** 下拉 */
const onRefresh = () => {
    if (refreFlag.value) {
        refreFlag.value = false
        finished.value = false; // 清空列表数据
        refreshing.value = false;
        loading.value = true; // 加载状态
        queryParams.value.pageNum = 1; // 分页数赋值为1
        list.value = []; // 清空数组
        onLoad(); // 重新加载数据
    } else {
        finished.value = false;
        refreshing.value = false;
    }
};
onMounted(() => { });
</script>
<style lang="less" scoped>
.product_list {
    width: 100%;
    height: 100vh;
    background-color: #f6faff;

    :deep(.van-nav-bar__content) {
        background-color: #fff;
        height: 46px !important;
        width: 100%;
    }

    .header {
        background: linear-gradient(#CDECFF 0%, #FFFFFF 70%);
        filter: blur(0px);
        height: 90px;
        padding-top: 44px;
        box-sizing: border-box;

        :deep(.van-icon-arrow-left) {
            color: #1a1b1c !important;
        }

        :deep(.van-nav-bar) {
            background: none !important;
        }

        :deep(.van-nav-bar__content) {
            background-color: transparent !important;
        }
    }

    .nothing_div {
        width: 100%;
        margin: auto;
        text-align: center;
        margin-top: 20vh;
        font-weight: 400;
        font-size: 12px;
        color: #B8BECA;
        line-height: 12px;
        flex-wrap: wrap;

        .van-image {
            width: 160px;
            height: 160px;
        }

    }

    // overflow: hidden;
    .product_list_serch-text {
        display: flex;
        align-items: center;
        width: 100%;
        background: #f6faff;
        height: 50px;
        position: relative;

        :deep(.van-search__field) {
            background-color: #fff;
            border-radius: 20px;
            padding-left: 15px;
        }

        .product_list_serch1 {
            display: flex;
            align-items: center;
            background: #f6faff;
            width: 100%;
            height: 35px;

            :deep(.van-search__content) {
                height: 35px;
            }

            :deep(.van-search__field) {
                height: 35px;
            }
        }

        .product_list_serch2 {
            position: absolute;
            right: 20px;

            :deep(.van-button__text) {
                font-size: 14px;
            }

            .product_list-btn {
                width: 54px;
                height: 26px;
                font-size: 14px;
                color: #ffffff;
                border: none;
                background: #0094ff;
                white-space: nowrap;
            }
        }
    }

    &_serch {
        display: flex;
        width: calc(100% - 20px);
        align-items: center;
        justify-content: space-between;
        padding: 0 10px;
        margin-top: 5px;
        margin-bottom: 5px;
        height: 23px;

        :deep(.van-image) {
            width: 20px;
            height: 20px;
        }

        &_item {
            border: 1px solid #a3a3a3;
            color: #a3a3a3;
            width: 60px;
            text-align: center;
            height: 20px;
            font-size: 12px;
            border-radius: 4px;
            line-height: 20px;
            overflow: hidden;
        }

        &_active {
            border: none !important;
            color: #fff;
            background-color: #1466ff;
        }
    }

    :deep(.van-pull-refresh) {
        height: calc(100vh - 140px);
        overflow: scroll;
    }

    :deep(.van-search) {
        background-color: #eeeeee;
    }

    &_van_list {
        // min-height: calc(100vh - 140px);
        padding: 0 10px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-wrap: wrap;

        &_div {
            width: 100%;
            height: 110px;
            box-sizing: border-box;
            background-color: #fff;
            border-radius: 3px;
            margin-bottom: 8px;
            padding: 5px;
            box-sizing: border-box;
            display: flex;
            justify-content: space-between;
        }

        .div_right {
            width: 100%;
            display: flex;
            flex-wrap: wrap;

            .div_right_top {
                height: 20px;
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: space-between;

                .style_name {
                    font-size: 14px;
                    font-weight: bold;
                }

                .stats {
                    text-align: center;
                    border-radius: 3px;
                    height: 22px;
                    background: #EBF4FF;
                    border-radius: 2px;
                    line-height: 22px;
                    font-weight: 500;
                    font-size: 12px;
                    color: #0094FF;
                    min-width: 70px;
                }
            }

            .div_right_center {
                width: 100%;
                height: 35px;
                color: #a3a3a3;
                font-size: 12px;
                display: flex;
                align-items: center;

                .contract {
                    div {
                        padding: 5px 10px;
                        box-sizing: border-box;
                        border-radius: 13px;
                        line-height: 1;
                    }
                }

                .contract_break {
                    margin-left: 15px;

                    div {
                        padding: 5px 10px;
                        box-sizing: border-box;
                        border-radius: 13px;
                        line-height: 1;
                    }
                }
            }

            .div_right_bottom {
                display: flex;
                width: 100%;
                align-items: center;
                justify-content: space-between;
                font-size: 12px;

                .div_right_bottom_div {
                    display: flex;
                    align-items: center;

                    .label {
                        width: 64px;
                        height: 22px;
                        background: #FFFFFF;
                        border-radius: 2px;
                        border: 1px solid #0094FF;
                        font-weight: 400;
                        font-size: 11px;
                        color: #0094FF;
                        text-align: center;
                        line-height: 22px;
                        box-sizing: border-box;
                    }

                    .value {
                        width: 40px;
                        text-align: center;
                        line-height: 22px;
                        height: 22px;
                        background: #EBF4FF;
                        border-radius: 2px;
                        font-weight: 500;
                        font-size: 12px;
                        color: #0094FF;
                    }
                }
            }
        }
    }
}
</style>